#!/bin/bash

# init host pre-reboot
# args:
#   $1: hostname: k8s
#   $2: host IP: 192.168.15.130
#   $3: gateway: 192.168.15.2
#   $4: prefix: 24
#   $5: nic name: ens33
#   $6: nameserver: 192.168.15.137

my_hostname=$1
my_ip=$2
my_gateway=$3
my_prefix=$4
eth_name=$5
my_nameserver=$6


function echo_g (){
    [ $# -ne 1 ] && return 1
    echo -e "\033[32m$1\033[0m"
}



echo_g "关闭防火墙、SELinux、DNSmasq、关闭Swap分区"
systemctl disable --now firewalld
systemctl disable --now dnsmasq
systemctl disable --now NetworkManager
setenforce 0
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

swapoff -a && sysctl -w vm.swappiness=0
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab


echo_g "设置时区、配置时间同步"
timedatectl set-timezone Asia/Shanghai
yum makecache fast
yum -y install chrony
systemctl enable --now chronyd



echo_g "set ulimit"
ulimit -SHn 65535
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65536
* hard nofile 131072
* soft nproc 65535
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
EOF


echo_g "配置静态IP"
echo $my_ip $(hostname) >> /etc/hosts
cat > /etc/sysconfig/network-scripts/ifcfg-$eth_name <<EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
IPADDR="$my_ip"
PREFIX="$my_prefix"
GATEWAY="$my_gateway"
DNS1="$my_nameserver"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="$eth_name"
DEVICE="$eth_name"
ONBOOT="yes"
EOF


echo_g "set hostname"
hostnamectl set-hostname $my_hostname


echo_g "nstall base packages and update system"
yum install -y epel-release
yum install -y make gcc gcc-c++ sudo patch tk autoconf gettext less vim wget jq psmisc socat \
               yum-utils device-mapper-persistent-data \
               lvm2 ipvsadm ipset sysstat conntrack \
               libseccomp nfs-utils python



echo_g "ssh 连接优化"
sed -ri 's/^#(UseDNS )yes/\1no/' /etc/ssh/sshd_config


echo_g "优化日志处理,减少磁盘IO"
sed -ri 's/^\$ModLoad imjournal/#&/' /etc/rsyslog.conf
sed -ri 's/^\$IMJournalStateFile/#&/' /etc/rsyslog.conf

sed -ri 's/^#(DefaultLimitCORE)=/\1=100000/' /etc/systemd/system.conf
sed -ri 's/^#(DefaultLimitNOFILE)=/\1=100000/' /etc/systemd/system.conf


echo_g "upgrade Linux kernel"
echo "192.168.1.60  webdav-share.gitee.cc" >> /etc/hosts
wget -rq -np -nH -R "index.html*" -k -e robots=off --no-check-certificate -m --cut-dirs=6 --http-user=admin --http-password=P@ssw0rd https://nginx-webdav-share.runjs.cn/public/kernel/el7/x86_64/RPMS/5.17.8/ -P ./5.17.8
cd ./5.17.8 \
    && yum localinstall -y kernel-ml* \
    && grub2-set-default  0 && grub2-mkconfig -o /etc/grub2.cfg \
    && grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)" 




echo_g " enable ipvs"
cat > /etc/sysconfig/modules/ipvs.modules <<EOF 
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4



echo_g "set sysctl config"
cat <<EOF > /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.may_detach_mounts = 1
net.ipv4.conf.all.route_localnet = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720

net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF
 
sysctl --system




echo
echo_g "重启"
echo
yum update -y --exclude=kernel* && reboot
